package com.example.hics_s.JavaFX;

import com.example.hics_s.Application.StringBootApplication;
import com.example.hics_s.Application.TextAreaOutputStream;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.OutputStream;
import java.io.PrintStream;

public class JavaFXappWindow extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        // 创建组件
        Text title = new Text("硬件信息收集系统(服务端)");
        Button StartProgram = new Button("启动程序");
        Button StopProgram = new Button("停止程序");
        Text Message = new Text("当前状态：服务未启动");

        Message.setFill(Color.RED);
        TextArea textArea = new TextArea();
        textArea.setEditable(false); // 设置为只读

        // 创建布局
        HBox hbox = new HBox(title);
        hbox.setStyle("-fx-font-size: 25px");
        hbox.setAlignment(Pos.BASELINE_CENTER);
        HBox hbox1 = new HBox(10, StartProgram, StopProgram);
        hbox1.setAlignment(Pos.BASELINE_CENTER);
        HBox hbox2 = new HBox(Message);
        hbox2.setAlignment(Pos.BASELINE_CENTER);
        HBox hbox3 = new HBox(textArea);
        hbox3.setAlignment(Pos.BASELINE_CENTER);

        VBox vBox = new VBox(20, hbox, hbox1, hbox2, hbox3);
        vBox.setStyle("-fx-padding: 30px");

        // 设置场景
        Scene scene = new Scene(vBox, 600, 550);
        stage.setScene(scene);
        stage.setTitle("硬件信息收集系统(HICS_S)");
        stage.show();

//         重定向输出
        OutputStream outputStream = new TextAreaOutputStream(textArea);
        System.setOut(new PrintStream(outputStream));
        System.setErr(new PrintStream(outputStream));

        // 启动按钮逻辑
        StartProgram.setOnAction(e -> {


            Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    if (StringBootApplication.context == null) {
                        String[] args = {};
                        StringBootApplication.main(args);
                    }
                    return null;
                }

                @Override
                protected void succeeded() {
                    Message.setText("当前状态：服务已启动");
                    Message.setFill(Color.GREEN);
                }

                @Override
                protected void failed() {
                    Message.setText("当前状态：启动失败");
                    Message.setFill(Color.RED);
                    Alert errorAlert = new Alert(Alert.AlertType.ERROR);
                    errorAlert.setTitle("错误");
                    errorAlert.setHeaderText("启动失败");
                    errorAlert.setContentText("错误信息: " + getException().getMessage());
                    errorAlert.showAndWait();
                }
            };
            new Thread(task).start();
        });

        // 停止按钮逻辑
        StopProgram.setOnAction(e -> {
            try {
                if (StringBootApplication.context != null) {
                    ((ConfigurableApplicationContext) StringBootApplication.context).close();
                    StringBootApplication.context = null;
                    Message.setText("当前状态：服务已停止");
                    Message.setFill(Color.RED);
                }
            } catch (Exception error) {
                Message.setText("当前状态：停止失败");
                Message.setFill(Color.RED);
                Alert errorAlert = new Alert(Alert.AlertType.ERROR);
                errorAlert.setTitle("错误");
                errorAlert.setHeaderText("停止失败");
                errorAlert.setContentText("错误信息: " + error.getMessage());
                errorAlert.showAndWait();
            }
        });

        // 处理窗口关闭事件
        stage.setOnCloseRequest(event -> {
            event.consume(); // 取消关闭事件

            // 弹出确认对话框
            Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION);
            confirmAlert.setTitle("确认关闭");
            confirmAlert.setHeaderText("您确定要关闭应用吗？");
            confirmAlert.setContentText("请确认。");

            confirmAlert.showAndWait().ifPresent(response -> {
                if (response == ButtonType.OK) {
                    // 停止服务
                    try {
                        if (StringBootApplication.context != null) {
                            ((ConfigurableApplicationContext) StringBootApplication.context).close();
                            StringBootApplication.context = null;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Platform.exit(); // 关闭应用
                }
            });
        });
    }
}